package com.net.framework.help.progress;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.net.framework.help.dialog.LoadAnimProgressDialog;

/**
 * Author：xiaobin.zhang
 * Email：154722566@qq.com
 * Date：2016/7/1 17:36
 * Desc：<--功能描述-->
 */
public class ProgressDialogHandler extends Handler {
    public static final int SHOW_PROGRESS_DIALOG = 1;
    public static final int DISMISS_PROGRESS_DIALOG = 2;
    //    private CommonProgressDialog pd;
    private LoadAnimProgressDialog loadAnimProgressDialog;
    private Context context;
    private boolean cancelable;
    private ProgressCancelListener mProgressCancelListener;

    public ProgressDialogHandler(Context context, ProgressCancelListener mProgressCancelListener, boolean cancelable) {
        super();
        this.context = context;
        this.mProgressCancelListener = mProgressCancelListener;
        this.cancelable = cancelable;
    }

    private void initProgressDialog() {
//        if (pd == null) {
//            pd = new CommonProgressDialog(context);
//            pd.setMessage("数据加载中...");
//            pd.setCanceledOnTouchOutside(false);//设置点击进度对话框外的区域对话框不消失
//            pd.setCancelable(cancelable);//设置进度条是否可以按退回键取消
//            if (cancelable) {
//                pd.setOnCancelListener(new DialogInterface.OnCancelListener() {
//                    @Override
//                    public void onCancel(DialogInterface dialogInterface) {
//                        mProgressCancelListener.onCancelProgress();
//                    }
//                });
//            }
//            if (!pd.isShowing()) {
//                pd.show();
//            }
//        }
        if (null == context) {
            return;
        }
        if (loadAnimProgressDialog == null) {
            loadAnimProgressDialog = new LoadAnimProgressDialog(context);
        }
        loadAnimProgressDialog.showProgressDialog(context, mProgressCancelListener, cancelable);
    }

    private void dismissProgressDialog() {
//        if (pd != null) {
//            pd.dismiss();
//            pd = null;
//        }
        if (loadAnimProgressDialog != null) {
            loadAnimProgressDialog.dismissProgressDialog();
            loadAnimProgressDialog = null;
        }
    }

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case SHOW_PROGRESS_DIALOG:
                initProgressDialog();
                break;
            case DISMISS_PROGRESS_DIALOG:
                dismissProgressDialog();
                break;
        }
    }
}
